﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Infrastructure.Web.Extensions;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;
using System.Web.Routing;
using System.Web.DynamicData;
using System.Security.Principal;
using Microsoft.Practices.Unity;
using Skynet.WebFoundation.Constants;
using Skynet.WebAdministracion.Constants;
using Skynet.BusinessEntities;
namespace Skynet.WebSite
{
    public class Global : System.Web.HttpApplication
    {
        private static MetaModel s_defaultModel = new MetaModel();
        public static MetaModel DefaultModel
        {
            get
            {
                return s_defaultModel;
            }
        }
        public static void Register(RouteCollection routes)
        {


            DefaultModel.RegisterContext(typeof(BusinessEntities.SkyNetLibraryEntities), new ContextConfiguration() { ScaffoldAllTables = false });            
            routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
            {
                Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                Model = DefaultModel
            });


            routes.MapPageRoute(ViewNames.Support.Names.SucessMessage(), "Support/SucessMessage", "~/Messages/SuccessMessageView.aspx", false);

            routes.MapPageRoute(Skynet.WebFoundation.Constants.RoutesNames.RuntimeExamStart, "Evaluation/StartExamViewer", "~/Evaluation/StartExamViewer.aspx", true);
            routes.MapPageRoute(Skynet.WebFoundation.Constants.RoutesNames.RuntimeExamViewer, "Evaluation/ExamViewer", "~/Evaluation/ExamViewer.aspx", true);
            routes.MapPageRoute(Skynet.WebFoundation.Constants.RoutesNames.DesignAdministration, "Administration/AdministrationMgmView", "~/Administration/AdministrationMgmView.aspx", true);
            routes.MapPageRoute(ViewNames.Administration.Names.DesignExamCategories(), "Administration/DesignCategories", "~/Administration/CategoriesMgmView.aspx", true, null, null, new RouteValueDictionary(new { RouteName = ViewNames.Administration.Names.DesignExamCategories() }));
            
            routes.MapPageRoute(Skynet.WebFoundation.Constants.RoutesNames.DesigMgmExams, "Administration/DesignMgmExams", "~/Administration/ExamMgmView.aspx", true);
            routes.MapPageRoute(Skynet.WebFoundation.Constants.RoutesNames.DesignExam, "Administration/DesignExams", "~/Administration/DesignStartExamView.aspx", true);
            routes.MapPageRoute(Skynet.WebFoundation.Constants.RoutesNames.DesignCustomerAdmin, "Administration/DesignCustomerAdmin", "~/Administration/CustomerMgmView.aspx", true);
            routes.MapPageRoute(Skynet.WebFoundation.Constants.RoutesNames.DesignExamActivation, "Administration/DesignExamActivation", "~/Administration/ExamActivationView.aspx", true);
            routes.MapPageRoute(Skynet.WebFoundation.Constants.RoutesNames.DesignQuestion, "Administration/DesignQuestion", "~/Administration/DesignExamViewerView.aspx", true);
            routes.MapPageRoute(Skynet.WebFoundation.Constants.RoutesNames.DesignAlternative, "Administration/DesignAlternative", "~/Administration/DesignAlternativeView.aspx", true);
            routes.MapPageRoute(Skynet.WebFoundation.Constants.RoutesNames.OrganizationMgm, "Administration/OrganizationMgm", "~/Administration/OrganizationMgmView.aspx", true);
            

            
            routes.MapPageRoute(ViewNames.Support.Names.ErrorMessage(), "Support/ErrorMessage", "~/Messages/ErrorMessageView.aspx", true);
            routes.MapPageRoute(Skynet.WebFoundation.Constants.RoutesNames.Home, "Home", "~/Default.aspx", true);                                                            
            
            routes.Add("owlvey-mobile-default", new Route("Mobile/Menu/WAP/default.wml", new Infrastructure.Web.Mobile.Routing.WmlRouteHandler<Mobile.Menu.WAP.defaulthandler>()));
            routes.Add("owlvey-mobile-CategoriesMgmView", new Route("Mobile/Menu/WAP/CategoriesMgmView.wml", new Infrastructure.Web.Mobile.Routing.WmlRouteHandler<Mobile.CategoriesMgmView.WAP.CategoriesMgmView>()));                       
        }

        void Application_Start(object sender, EventArgs e)
        {
            Register(RouteTable.Routes);    

            var container = HttpContext.Current.Application.GetContainer();            
            var AspectPath = Server.MapPath("~/Config/Aspect.config");
            var AspectMap = new ExeConfigurationFileMap() { ExeConfigFilename = AspectPath };
            var AspectSection = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(AspectMap, ConfigurationUserLevel.None).GetSection("unity") as UnityConfigurationSection;
            container.LoadConfiguration(AspectSection);

            var DataAccessPath = Server.MapPath("~/Config/DataAccess.config");
            var DataAccessMap = new ExeConfigurationFileMap() { ExeConfigFilename = DataAccessPath };
            var DataAccessSection = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(DataAccessMap, ConfigurationUserLevel.None).GetSection("unity") as UnityConfigurationSection;
            container.LoadConfiguration(DataAccessSection);

            var BusinessPath = Server.MapPath("~/Config/Business.config");
            var BusinessMap = new ExeConfigurationFileMap() { ExeConfigFilename = BusinessPath };
            var BusinessSection = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(BusinessMap, ConfigurationUserLevel.None).GetSection("unity") as UnityConfigurationSection;
            container.LoadConfiguration(BusinessSection);

            var AdministracionPath = Server.MapPath("~/Config/Administracion.config");
            var AdministracionMap = new ExeConfigurationFileMap() { ExeConfigFilename = AdministracionPath };
            var AdministracionSection = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(AdministracionMap, ConfigurationUserLevel.None).GetSection("unity") as UnityConfigurationSection;
            container.LoadConfiguration(AdministracionSection);
        }
        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown
        }

        void Application_Error(object sender, EventArgs e)
        {
            
        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
        }

        void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            //if (HttpContext.Current.Request.IsAuthenticated)
            //{ 
            //    var username = HttpContext.Current.User.Identity.Name;
            //    if (username == "greg")
            //    {
                    
            //        var prin = new GenericPrincipal(HttpContext.Current.User.Identity, new string[] { "Creators", "Admins" });
            //        HttpContext.Current.User = prin;
            //    }
            //}
        }

    }
}
